import { create } from "zustand";
import { persist } from "zustand/middleware";
import { produce } from "immer";
import { storage } from "./config";

const useUserStore = create(
  persist(
    (set) => ({
      user: {},
      isAuthenticated: false,
      setUser: (user) => set({ user, isAuthenticated: true }),
      updateUser: (name, age) =>
        set(
          produce((state) => {
            state.user.name = name;
            state.user.age = age;
          })
        ),
      removeUser: () => set({ user: {}, isAuthenticated: false }),
    }),
    {
      name: "user-storage",
      storage,
    }
  )
);
export default useUserStore;
